const fs = require("fs");
const execa = require("execa"); //node 开启多线程
const exec = require("child_process").execSync;

const targets = fs
  .readdirSync("packages")
  .filter((f) => fs.statSync(`packages/${f}`).isDirectory());
// console.log(targets);
async function build(target) {
  // console.log(target, "target");
  await execa("rollup", ["-c", "--environment", `TARGET:${target}`], {
    stdio: "inherit", //子进程的打包信息，共享给父进程
  });
  // 使用原生child_process的execSync
  //   await exec(`rollup -c --environment TARGET:${target}`, {
  //     stdio: "inherit",
  //   });
}
function runParallel(targets, iteratorFn) {
  const res = [];
  for (let item of targets) {
    const p = iteratorFn(item);
    res.push(p);
  }
  return Promise.all(res);
}
runParallel(targets, build);
